package com.pig4cloud.pig.model.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.model.api.entity.ModelSortEntity;
import com.pig4cloud.pig.model.service.ModelSortService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 模型分类表
 *
 * @author wangyl
 * @date 2024-06-06 13:59:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/modelSort" )
@Tag(description = "modelSort" , name = "模型分类表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ModelSortController {

    private final  ModelSortService modelSortService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param modelSort 模型分类表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('model_modelSort_view')" )
    public R getModelSortPage(@ParameterObject Page page, @ParameterObject ModelSortEntity modelSort) {

        return R.ok(modelSortService.getListpage(page, modelSort.getName(),modelSort.getStatus()));
    }


    /**
     * 通过id查询模型分类表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('model_modelSort_view')" )
    public R getById(@PathVariable("id" ) Long id) {

    	ModelSortEntity modelSort = modelSortService.getBySortId(id);
        return R.ok(modelSort);
    }

    /**
     * 新增模型分类表
     * @param modelSort 模型分类表
     * @return R
     */
    @Operation(summary = "新增模型分类表" , description = "新增模型分类表" )
    @SysLog("新增模型分类表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('model_modelSort_add')" )
    public R save(@RequestBody ModelSortEntity modelSort) {
    	modelSort.setDelFlag(0);
        return R.ok(modelSortService.save(modelSort));
    }

    /**
     * 修改模型分类表
     * @param modelSort 模型分类表
     * @return R
     */
    @Operation(summary = "修改模型分类表" , description = "修改模型分类表" )
    @SysLog("修改模型分类表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('model_modelSort_edit')" )
    public R updateById(@RequestBody ModelSortEntity modelSort) {
        return R.ok(modelSortService.updateById(modelSort));
    }

    /**
     * 通过id删除模型分类表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除模型分类表" , description = "通过id删除模型分类表" )
    @SysLog("通过id删除模型分类表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('model_modelSort_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(modelSortService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param modelSort 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('model_modelSort_export')" )
    public List<ModelSortEntity> export(ModelSortEntity modelSort,Long[] ids) {
        return modelSortService.list(Wrappers.lambdaQuery(modelSort).in(ArrayUtil.isNotEmpty(ids), ModelSortEntity::getId, ids));
    }
}
